<template>
  <div class="pages receiptEdit">
    <div class="operationBox tableSearch">
      <el-button type="primary" @click="goBack()" size="mini">返回</el-button>
      <el-button type="primary" size="mini" @click="centerDialogVisible = true">确认收款</el-button>
    </div>
    <el-form :model="formdata" ref="formdata" label-width="120px" size="mini" class="box-content">
      <el-row>
        <el-col :span="8">
          <el-form-item label="供应商 :" prop="supplierName" v-if="paymentStatusArray=='退货收款'">
            <span>{{formdata.supplierName}}</span>
          </el-form-item>
          <el-form-item label="企业名称 :" prop="supplierName" v-if="paymentStatusArray=='销售收款'">
            <span>{{formdata.shopName}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单号 :">
            <span>{{formdata.saleCode}}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据日期 :" prop="receiveDate">
            <span>{{formdata.orderDate}}</span>
            <!-- <el-date-picker v-if="showTab === 2" v-model="formdata.receiveDate" format="yyyy年 MM 月 dd 日" value-format="yyyy-MM-dd" type="date" placeholder="选择交货日期"></el-date-picker> -->
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="付款类型 :">
            <span>{{paymentStatusArray}}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="订单状态 :">
            <span>{{saleStatusArray}}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 订单信息 -->
    <div class="from_main infon">
      <div class="infon_text">
        <span class="infon_shu"></span>
        <span class="infon_text_t">订单信息</span>
      </div>
    </div>
    <cope-with :formdata="formdata" :confirmtype="confirmtype" :tableData="tableData" :isConfirm="isConfirm" :hasConfirmCai="hasConfirmCai" style="margin-top:20px;"></cope-with>
    <el-dialog title="确认收款" class="addshopname" :visible.sync="centerDialogVisible" width="470px">
      <div class="wxser">
        <div>企业名称：{{payerName}}</div>
        <div class="span-left">付款时间：{{orderTime}}</div>
      </div>
      <div class="wxser">
        <div>订单总金额：{{orderPrice}} 元</div>
        <div class="span-left">付款金额：{{price}} 元</div>
      </div>
      <div class="tips">
        是否确认收到款项？
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="receiptConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState } from "vuex";
import SdInput from "@/components/SdInput";
import pageUnit from "components/Page";
import CopeWith from "@/components/CopeWith";
import u from "@/util";
import {
  selectGoodsSaleDetails,
  selectOrderGoodsPurchaseReturnDetail,
  receiptConfirm
} from "@/api";
export default {
  components: { SdInput, pageUnit, CopeWith },
  data() {
    return {
      paymentStatusArray: "", // 收付款状态数组
      saleStatusArray: "", // 订单状态数组
      centerDialogVisible: false,
      formdata: {
        purchaseCode: "",
        deliveryDate: "", // 交货日期
        purchaseStatus: "", // 订单状态
        orderDate: "", // 单据日期
        receivedStatus: "", // 发货状态
        payStatus: "", // 收款状态
        shopName: "", //商户名称
        shopId: "", // 客户id
        goodsList: [], // 物料
        creator: "", // 操作人
        remark: "", // 备注
        // purchaseAmount: 0, // 总计
        freight: 0, // 运费
        taxAmount: 0, // 税
        amount: 0, // 实收总计
        confirmSaleAmount: 0,
        confirmFreight: 0,
        confirmTax: 0,
        confirmAmount: 0,
        saleType: ""
      },
      tableData: [],
      isConfirm: true, // 是否进行过报货确认
      confirmtype: false,
      hasConfirmCai: false, // 是否是采购报货
      inStatus: ["已生成", "", "已退货"]
    };
  },
  computed: mapState([
    "sub",
    "merchantId",
    "power",
    "path",
    "linkName",
    "mobile",
    "userId"
  ]),
  created() {
    this.payNo = u.getQuery().payNo; // 查看id
    this.paymentType = u.getQuery().paymentType;
    this.salesId = u.getQuery().salesId;
    this.purchaseId = u.getQuery().purchaseId;
    this.payerName = u.getQuery().payerName;
    let time = u.getQuery().orderTime;
    this.orderTime = this.getdatetime(parseInt(time));
    this.orderPrice = u.getQuery().orderPrice;
    this.price = u.getQuery().price;
  },
  mounted() {
    this.getDataById();
  },
  methods: {
    getdatetime(now, l = "-", m = ":") {
      // now = parseInt(now);
      now = new Date(now);
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      let date = now.getDate();
      let hour = now.getHours();
      let minute = now.getMinutes();
      // let seconds = now.getSeconds()
      if (month < 10) {
        month = "0" + month;
      }
      if (date < 10) {
        date = "0" + date;
      }
      if (hour < 10) {
        hour = "0" + hour;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      return year + l + month + l + date;
    },
    receiptConfirm() {
      let postdata = {
        payNo: this.payNo
      };
      receiptConfirm(postdata).then(res => {
        if (res.code == 200) {
          this.$message({
            showClose: true,
            message: "确认收款成功",
            type: "success"
          });
          this.centerDialogVisible = false;
        }
      });
    },
    getdatetime(now, l = "-", m = ":") {
      now = new Date(now);
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      let date = now.getDate();
      let hour = now.getHours();
      let minute = now.getMinutes();
      // let seconds = now.getSeconds()
      if (month < 10) {
        month = "0" + month;
      }
      if (date < 10) {
        date = "0" + date;
      }
      if (hour < 10) {
        hour = "0" + hour;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      return year + l + month + l + date;
    },
    selectGoodsSaleDetails(id) {
      let data = {
        id: id
      };
      selectGoodsSaleDetails(data).then(res => {
        this.formdata = JSON.parse(JSON.stringify(res.data));
        let a = [
          "待确认",
          "待收款",
          "已取消",
          "待发货",
          "已关闭",
          "已清",
          "采购方取消订单",
          "采购方关闭订单",
          "待收货"
        ]; //销售订单状态
        this.saleStatusArray = a[res.data.statusFlow];
        this.formdata.saleCode = res.data.saleCode;
        // this.formdata.supplierName = res.data.shopName;
        this.formdata.purchaseAmount = res.data.saleAmount;
        this.formdata.retreatData = true;
        this.formdata.retreatDataname = "应收款确认";
        this.formdata.orderDate = this.getdatetime(res.data.orderDate);
        // console.log(res.data.purchaseCode);
        this.tableData = res.data.goodsList;
        for (let i = 0; i < this.tableData.length; i++) {
          this.tableData[i].goodsNum = this.tableData[i].purchaseNum;
        }
      });
    },
    getDataBy(goodsSaleId) {
      let data = {
        id: goodsSaleId
      };
      selectOrderGoodsPurchaseReturnDetail(data).then(res => {
        this.formdata = JSON.parse(JSON.stringify(res.data));
        let a = ["已生成", "未同意", "待发货", "待收货", "待退款", "已完成"]; //退货订单状态
        this.saleStatusArray = a[this.formdata.purchaseStatus];
        this.formdata.retreatData = false;
        this.formdata.saleAmount = this.formdata.returnAmount;
        this.tableData = res.data.goodsList;
        this.formdata.purchaseAmount = res.data.formdataTotal;
        this.formdata.orderDate = this.getdatetime(res.data.orderDate);
        this.formdata.saleCode = res.data.purchaseinCode;
      });
    },
    getDataById() {
      let b = ["", "销售收款", "退货收款"];
      this.paymentStatusArray = b[this.paymentType];
      if (this.paymentType == 1) {
        this.selectGoodsSaleDetails(this.salesId);
      } else if (this.paymentType == 2) {
        this.getDataBy(this.purchaseId);
      }
    },
    // 分配权限
    setRole(key) {
      return u.selectTitle(this.power, key);
    },
    // 处理审核状态

    // 点击返回按钮
    goBack() {
      history.back();
    }
  }
};
</script>
<style scoped>
.th-right {
  margin-top: 30px;
}
.from_main {
  margin-top: 10px;
  margin-bottom: 30px;
}
.box-content {
  margin-top: 10px;
}
.wxser {
  line-height: 50px;
  margin-left: 10px;
  height: 40px;
  width: 420px;
}
.wxser div {
  float: left;
  width: 200px;
}
.wxser .span-left {
  margin-left: 20px;
}
.tips {
  text-align: center;
  margin: 30px 30px 0px 30px;
}
</style>
